<template>
  <div class="elm-edit">
    <elm-page-bb>
      <el-form ref="model" :model="model" size="small" label-width="80px">
        <elm-row>
          <el-form-item label="结算编号" prop="js_code">
            <el-input v-model="model.js_code"></el-input>
          </el-form-item>
          <el-form-item label="客户名称" prop="kh">
            <elm-my-customer v-model="model.kh" :name="model.kh"></elm-my-customer>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="联系方式" prop="lxfs">
            <el-input v-model="model.lxfs"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="dz">
            <el-input v-model="model.dz"></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="关联项目" prop="pj_id">
            <elm-select-input v-model="model.pj_id" url="/business/my/GetPjData" :readonly="false" :show-id="true">
            </elm-select-input>
          </el-form-item>
          <el-form-item label="关联订单" prop="order_id">
            <elm-select-input v-model="model.order_id" url="/business/my/GetOrderData" :readonly="false"
                              :show-id="true">
            </elm-select-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="关联合同" prop="contract_id" v-if="type === 'in'">
            <elm-select-input v-model="model.contract_id" url="/business/my/GetContractData" :readonly="false"
                              :show-id="true">
            </elm-select-input>
          </el-form-item>
          <el-form-item label="关联合同" prop="contract_id" v-else>
            <elm-select-input v-model="model.contract_id" url="/business/my/GetContractZcData" :readonly="false"
                              :show-id="true">
            </elm-select-input>
          </el-form-item>
          <el-form-item label="结算时间" prop="js_date">
            <el-date-picker v-model="model.js_date" type="datetime" value-format="yyyy-MM-ddTHH:mm:ss"
                            style="width: 100%">
            </el-date-picker>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="结算状态" prop="js_status">
            <elm-code dict_type="bms_settlement_zt" v-model="model.js_status"></elm-code>
          </el-form-item>
          <el-form-item label="结算金额" prop="js_amount" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model="model.js_amount">
              <template slot="append">
                <div style="width: 25px;text-align: center;">万元</div>
              </template>
            </el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="发票附件">
            <elm-upload module_name="biz_bms_settlement_file" :rel_id="model.id" :multiple="true" :limit="10"
                        list-type="file" tip=""></elm-upload>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="发票税号">
            <el-input v-model="model.fp_sh"></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="跟进人" prop="sk_person">
            <elm-ei v-model="model.sk_person"></elm-ei>
          </el-form-item>
        </elm-row>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="2" v-model="model.remark"></el-input>
        </el-form-item>
      </el-form>
      <template #bottom>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      model: {
        id: elm.newGuid(),
        contract_id: null
      },
      // 合同编号
      code: elm.dialog.request.code,
      // 合同类型 in收入/out支出
      type: elm.dialog.request.type,
    }
  },
  created() {
    this.$nextTick(() => {
      this.getList();
    })
  },
  methods: {
    //查询合同编号关联的结算数据
    async getList() {
      let url = '/business/bms/settlement/GetSingle';
      if (this.type === 'out') {
        url = '/business/bms/settlement_zc/GetSingle';
      }
      //查询结算情况
      let res = await elm.post(url, { code: this.code });
      if (res) {
        // this.model = res;
        this.model = {
          ...res,
          contract_id: res.contract_id || this.code
        };
      } else {
        this.model.contract_id = this.code;
      }
    },
    async submit() {
      await this.$refs["model"].validate();
      let url = '/business/bms/settlement/Upsert';
      if (this.type === 'out') {
        url = '/business/bms/settlement_zc/Upsert';
      }
      var res = await elm.post(url, this.model);
      if (res == true) {
        this.v('保存成功。')
        this.close();
        this.request.callVue.tablebinding();
      } else {
        this.error('保存失败，请稍后尝试。');
      }
    },
  }
}
</script>